package com.cuit.zuke.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.zuke.common.exception.GlobalException;
import com.cuit.zuke.common.util.PageUtil;
import com.cuit.zuke.system.dao.SysLogDao;
import com.cuit.zuke.system.entity.SysLogEntity;
import com.cuit.zuke.system.service.SysLogService;
import org.omg.CORBA.SystemException;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @program: zuke
 * @description: 系统日志服务接口实现类
 * @author: KamTang
 * @create: 2022-04-15 14:58
 **/
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogDao, SysLogEntity> implements SysLogService {


    /**
     * 异步保存操作日志
     *
     * @param log
     */
    @Override
    public void saveLog(SysLogEntity log) {
        this.baseMapper.insert(log);
    }

    /**
     * 删除登入日志
     *
     * @param id
     * @throws SystemException
     */
    @Override
    public void delete(Long id) throws SystemException {
        SysLogEntity selectById = this.baseMapper.selectById(id);
        if (selectById == null) {
            throw new GlobalException("要删除的日志不存在", 500);
        }
        this.baseMapper.deleteById(id);
    }

    /**
     * 日志列表
     *
     * @param params
     * @return
     */
    @Override
    public PageUtil findLogList(Map<String, Object> params) {
        // 获取前端当前页
        long currentPage = Long.parseLong((String) params.get("currentPage"));
        // 每页最多数据数
        long pageSize = Long.parseLong((String) params.get("pageSize"));
        QueryWrapper<SysLogEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        // 获取前端具体参数
        String username = (String) params.get("username");
        String ip = (String) params.get("ip");
        String location = (String) params.get("location");
        if (username != null && !"".equals(username)) {
            wrapper.like("username", "%" + username + "%");
        }
        if (ip != null && !"".equals(ip)) {
            wrapper.like("ip", "%" + ip + "%");
        }
        if (location != null && !"".equals(location)) {
            wrapper.like("location", "%" + location + "%");
        }
        Page<SysLogEntity> logEntityPage = new Page<>(currentPage, pageSize);
        IPage<SysLogEntity> iPage = baseMapper.selectPage(logEntityPage, wrapper);
        return new PageUtil(iPage);
    }

    /**
     * 批量删除日志
     *
     * @param ids
     * @throws SystemException
     */
    @Override
    public void deleteBatch(Long[] ids) throws SystemException {
        for (Long id : ids) {
            SysLogEntity sysLogEntity = this.baseMapper.selectById(id);
            if (sysLogEntity == null) {
                throw new GlobalException("id=" + id + "操作日志不存在", 500);
            }
            this.baseMapper.deleteById(id);
        }
    }
}
